﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Imaginecup.sprite;
using Microsoft.Xna.Framework;
using imaginecup.util;
using Imaginecup.util.resourceMan;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Content;
using Imaginecup.util;
using Imaginecup.gameState.intro;
using Imaginecup.gameState.btnStateUtil;
using Imaginecup.util.collision;

namespace Imaginecup.gameState.option
{
    public class ResetState : GameState// , ISelectBtnState
    {
        enum ResetEnum { UpScroll , None , DownScroll};

        private ContentManager m_MenuContent;     
                
        private ISprite m_BackSprite;
        private ISprite m_AlphaSprite;


        private ISprite m_YesBtnSprite;
        private ISprite m_NoBtnSprite;

        private EnabletBtnInfo m_YesBtnInfo;
        private EnabletBtnInfo m_NoBtnInfo;

        private double m_ScrollPosY;

        private ResetEnum m_State;
        private int m_ScrollSpeed = 550;
        private int m_InitPosY = 366;

        private static ResetState m_ResetState = null;

        private ResetState()
        {
            m_MenuContent = new ContentManager(m_Game.Services);
            m_MenuContent.RootDirectory = "Content";

        }



        public static ResetState GetOptionResetState()
        {
            if (m_ResetState == null)
            {
                m_ResetState = new ResetState();
            }
            return m_ResetState;
        }


        /************************************************************************/
        /* 초기화 되는 값(메뉴인덱스)                                                       */
        /************************************************************************/
        protected override void InitState()
        {
            m_ScrollPosY = m_InitPosY;

            m_State = ResetEnum.UpScroll;


            Vector2 btnSize = new Vector2(240 , 206);

            BtnInfo btnInfoYes = new BtnInfo(new EntityInfo(new Vector2(163 , 268) , btnSize) , false);
            BtnInfo btnInfoNo = new BtnInfo(new EntityInfo(new Vector2(408 , 262) , btnSize) , false);

            m_YesBtnInfo = new EnabletBtnInfo();
            m_NoBtnInfo = new EnabletBtnInfo();

            m_YesBtnInfo.SetBtnInfo(btnInfoYes);
            m_NoBtnInfo.SetBtnInfo(btnInfoNo );

        }


        /************************************************************************/
        /* ContentMan에 리소스 로드(배경,메뉴 리소스)                                  */
        /************************************************************************/
        protected override void LoadStateResource()
        {
            ResourceMan.GetResourceMan().SetTextureFrame("ResetBack", m_MenuContent.Load<Texture2D>("option\\option_reset"));
            ResourceMan.GetResourceMan().SetTextureFrame("ResetBack_Alpha", m_MenuContent.Load<Texture2D>("option\\option_reset_Alpha"));
            ResourceMan.GetResourceMan().SetTextureFrame("ResetYes", m_MenuContent.Load<Texture2D>("option\\reset_yes"));
            ResourceMan.GetResourceMan().SetTextureFrame("ResetNo", m_MenuContent.Load<Texture2D>("option\\reset_no"));

            NormalSprite normalSprite = new NormalSprite();
            normalSprite.SetTextureName("ResetBack");
            m_BackSprite = normalSprite;

            normalSprite = new NormalSprite();
            normalSprite.SetTextureName("ResetBack_Alpha");
            m_AlphaSprite = normalSprite;

            ChangeSourceSizeSprite changeSprite = new ChangeSourceSizeSprite();
            changeSprite.SetSourceInfoClass(m_NoBtnInfo);
            changeSprite.SetTextureName("ResetNo");

            m_NoBtnSprite = changeSprite;

            changeSprite = new ChangeSourceSizeSprite();
            changeSprite.SetSourceInfoClass(m_YesBtnInfo);
            changeSprite.SetTextureName("ResetYes");

            m_YesBtnSprite = changeSprite;
        }


        /************************************************************************/
        /* 리소스해제                                                                     */
        /************************************************************************/
        protected override void UnloadResource()
        {
            m_BackSprite.Release();
            m_AlphaSprite.Release();
            m_MenuContent.Unload();

            m_YesBtnInfo.Release();
            m_NoBtnInfo.Release();

            m_YesBtnInfo = null;
            m_NoBtnInfo = null;

            m_YesBtnSprite.Release();
            m_NoBtnSprite.Release();

            ResourceMan.GetResourceMan().Release("ResetBack");
            ResourceMan.GetResourceMan().Release("ResetBack_Alpha");
            ResourceMan.GetResourceMan().Release("ResetYes");
            ResourceMan.GetResourceMan().Release("ResetNo");
        }

        

        public override void Update(Microsoft.Xna.Framework.GameTime gameTime)
        {
            if (m_State == ResetEnum.UpScroll)
            {
                m_ScrollPosY -= gameTime.ElapsedGameTime.TotalSeconds * m_ScrollSpeed;
                
                if (m_ScrollPosY < 0)
                {
                    m_ScrollPosY = 0;
                    m_State = ResetEnum.None;

                    m_NoBtnInfo.GetBtnInfo().SetEnable(true);
                    m_YesBtnInfo.GetBtnInfo().SetEnable(true);
                }
            }

            else if( m_State == ResetEnum.None )
            {
                
                IMouseInputHandler mouseInput = MouseInputHandler.GetInputHandler();
                if (mouseInput.IsOneClickUp() && Collision.CollisionByPoint(m_YesBtnInfo.GetEntityInfo() , mouseInput.Position))
                {
                    m_State = ResetEnum.DownScroll;

                    m_NoBtnInfo.GetBtnInfo().SetEnable(false);
                    m_YesBtnInfo.GetBtnInfo().SetEnable(false);
                }
                else if (mouseInput.IsOneClickUp() && Collision.CollisionByPoint(m_NoBtnInfo.GetEntityInfo(), mouseInput.Position))
                {
                    m_State = ResetEnum.DownScroll;

                    m_NoBtnInfo.GetBtnInfo().SetEnable(false);
                    m_YesBtnInfo.GetBtnInfo().SetEnable(false);
                }

                m_NoBtnInfo.Update();
                m_YesBtnInfo.Update();
            }

            else 
            {
                m_ScrollPosY += gameTime.ElapsedGameTime.TotalSeconds * m_ScrollSpeed;

                if( m_ScrollPosY > m_InitPosY)
                {
                    m_ScrollPosY = m_InitPosY;

                    m_GameStateMan.PopState();
                }
            }
            base.Update(gameTime);
        }

        

        public override void Draw(GameTime gameTime)
        {
            SpriteBatch sb = m_GraphicData.GetSpriteBatch();

            m_AlphaSprite.Draw(sb, new EntityInfo(0, 0, 800, 480));
            m_BackSprite.Draw(sb, new EntityInfo(0, (int)m_ScrollPosY, 800, 480));

            EntityInfo yesBtnInfo = m_YesBtnInfo.GetEntityInfo();
            EntityInfo noBtnInfo = m_NoBtnInfo.GetEntityInfo();

            yesBtnInfo.mPosition.Y += (int)m_ScrollPosY;
            noBtnInfo.mPosition.Y += (int)m_ScrollPosY;

            m_YesBtnSprite.Draw(sb, yesBtnInfo);
            m_NoBtnSprite.Draw(sb, noBtnInfo);
                                 
            base.Draw(gameTime);
        }


        //public void StateChange(int menuIndex)
        //{
        //    switch (menuIndex)
        //    {
        //        case 0:
        //            ChangeState((IGameState)StateFactory.GetStateFactory().GetObject(StateFactoryID.State_TitleMenu));
        //            return;                       
        //    }
        //}

        //public int GetMaxMenu()
        //{
        //    return MAX_MENU;
        //}

        //public BtnInfo[] GetBtnInfoArray()
        //{
        //    return m_MenuButton;
        //}

        //public ISprite[] GetSpriteArray()
        //{
        //    return m_BtnSprite;
        //}
    }
}
